package de.bk.timecalc.gui;

import static de.bk.timecalc.gui.GuiToolbox.*;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;

import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;

import de.bk.timecalc.gui.GuiToolbox.DefaultSelectionListener;

class HelpWindow
{
  private static final int OPTIONS_SHELL_WIDTH = 700;
  private static final int OPTIONS_SHELL_HEIGHT = 700;

  private Shell shell;
  private GridData gridDataBrowser;

  HelpWindow()
  {
    super();
  }

  void init(Shell mainShell)
  {
    this.createShell(mainShell);
    this.createBrowserHelp();
    this.createButtonClose();
  }

  private void createShell(Shell mainShell)
  {
    this.shell = new Shell(mainShell, SWT.MIN | SWT.MAX | SWT.TITLE);
    this.shell.setText("Help");
    this.shell.setSize(OPTIONS_SHELL_WIDTH, OPTIONS_SHELL_HEIGHT);
    this.shell.setImage(new Image(mainShell.getDisplay(), TimeCalcGui.class
        .getResourceAsStream(GameWindowLayout.SHELL_ICON_IMAGE)));
    GridLayout layoutShell = new GridLayout(1, false);
    this.shell.setLayout(layoutShell);
    this.shell.addListener(SWT.Close, new Listener()
    {
      @Override
      public void handleEvent(Event event)
      {
        HelpWindow.this.hide();
        event.doit = false;
      }
    });
  }

  private void createBrowserHelp()
  {
    this.gridDataBrowser = new GridData();
    this.gridDataBrowser.horizontalAlignment = SWT.CENTER;
    this.gridDataBrowser.verticalAlignment = SWT.CENTER;
    this.gridDataBrowser.widthHint = OPTIONS_SHELL_WIDTH - 20;
    this.gridDataBrowser.heightHint = OPTIONS_SHELL_HEIGHT - 50;
    this.gridDataBrowser.grabExcessVerticalSpace = true;
    this.gridDataBrowser.grabExcessHorizontalSpace = true;
    Browser browserHelp = new Browser(this.shell, SWT.NONE);
    browserHelp.setLayoutData(this.gridDataBrowser);

    URL htmlUrl = HelpWindow.class.getResource("help.html");
    browserHelp
        .setText("<html><head></head><body>no help available</body></html>");
    if (htmlUrl != null)
    {
      try
      {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
            htmlUrl.openStream()));
        StringBuilder html = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null)
        {
          html.append(line);
          html.append(" ");
        }
        browserHelp.setText(html.toString());
      }
      catch (IOException e)
      {
        throw new RuntimeException(e);
      }
    }
  }

  private void createButtonClose()
  {
    GridData gridData = new GridData();
    gridData.horizontalAlignment = SWT.CENTER;
    gridData.verticalAlignment = SWT.CENTER;
    Button buttonClose = createButton(this.shell, "Close");
    buttonClose.setLayoutData(gridData);
    buttonClose.addSelectionListener(new DefaultSelectionListener()
    {
      @Override
      public void widgetSelected(SelectionEvent e)
      {
        HelpWindow.this.hide();
      }
    });
  }

  void show()
  {
    this.shell.open();
  }

  void hide()
  {
    this.shell.setVisible(false);
  }
}
